package soms_handler

import (
	"encoding/json"
	"gitlab.yoyiit.com/youyi/go-core/handler"
	"gitlab.yoyiit.com/youyi/go-core/util"
	"github.com/gin-gonic/gin"
	"github.com/pkg/errors"
	"strconv"
	"time"
)

type ProductCustomFieldListColumnHandler interface {
	ListProductCustomFieldListColumn() gin.HandlerFunc
	GetProductCustomFieldListColumn() gin.HandlerFunc
	AddProductCustomFieldListColumn() gin.HandlerFunc
	EditProductCustomFieldListColumn() gin.HandlerFunc
	DeleteProductCustomFieldListColumn() gin.HandlerFunc
	BatchDeleteProductCustomFieldListColumn() gin.HandlerFunc
}

type productCustomFieldListColumnHandler struct {
	somsClient soms.Client
}

func (h *productCustomFieldListColumnHandler) ListProductCustomFieldListColumn() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		var req stru.ListProductCustomFieldListColumnDataRequest
		if err := ctx.ShouldBindQuery(&req); err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		res, err := h.somsClient.ListProductCustomFieldListColumn(util.GetRPCContext(ctx), &api.ListProductCustomFieldListColumnRequest{
			PageNum:  req.PageNum,
			PageSize: req.PageSize,
			Data: &api.ProductCustomFieldListColumnData{
				ProductCustomFieldId: req.ProductCustomFieldId,
	ColumnName: req.ColumnName,
	MasterFlag: req.MasterFlag,
	
			},
		})
		if err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		if res == nil || res.Data == nil {
			handler.HandleSuccessListDataResponse(ctx, nil, 0)
			return
		}
		handler.HandleSuccessListDataResponse(ctx, res.Data, res.Count)
	}
}

func (h *productCustomFieldListColumnHandler) GetProductCustomFieldListColumn() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		id, err := strconv.ParseInt(ctx.Param("id"), 10, 0)
		if err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		res, err := h.somsClient.GetProductCustomFieldListColumn(util.GetRPCContext(ctx), &api.ProductCustomFieldListColumnData{
			Id: id,
		})
		if err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		handler.HandleSuccessDataResponse(ctx, res)
	}
}

func (h *productCustomFieldListColumnHandler) AddProductCustomFieldListColumn() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		var data stru.ProductCustomFieldListColumnData
		if err := ctx.ShouldBindJSON(&data); err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		addData := &api.ProductCustomFieldListColumnData{
            Id:         data.Id,
            ProductCustomFieldId: data.ProductCustomFieldId,
	ColumnName: data.ColumnName,
	MasterFlag: data.MasterFlag,
	
        }
		err := h.somsClient.AddProductCustomFieldListColumn(util.GetRPCContext(ctx), addData)
		if err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		handler.HandleSuccessResponse(ctx)
	}
}

func (h *productCustomFieldListColumnHandler) EditProductCustomFieldListColumn() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		id, err := strconv.ParseInt(ctx.Param("id"), 10, 0)
		if err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		var data stru.ProductCustomFieldListColumnData
		if err = ctx.ShouldBindJSON(&data); err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}

		editData := &api.ProductCustomFieldListColumnData{
            Id:         id,
            ProductCustomFieldId: data.ProductCustomFieldId,
	ColumnName: data.ColumnName,
	MasterFlag: data.MasterFlag,
	
        }
		if err = h.somsClient.EditProductCustomFieldListColumn(util.GetRPCContext(ctx), editData); err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		handler.HandleSuccessResponse(ctx)
	}
}

func (h *productCustomFieldListColumnHandler) DeleteProductCustomFieldListColumn() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		id, err := strconv.ParseInt(ctx.Param("id"), 10, 0)
		if err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		if err = h.somsClient.DeleteProductCustomFieldListColumn(util.GetRPCContext(ctx), id); err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		handler.HandleSuccessResponse(ctx)
	}
}

func (h *productCustomFieldListColumnHandler) BatchDeleteProductCustomFieldListColumn() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		var req stru.BatchDeleteProductCustomFieldListColumnRequest
		if err := ctx.ShouldBindJSON(&req); err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}

		if req.Ids == nil || len(req.Ids) == 0 {
			handler.HandleErrorResponse(ctx, handler.HandleNewError("参数错误"))
			return
		}

		if err := h.somsClient.BatchDeleteProductCustomFieldListColumn(util.GetRPCContext(ctx), &api.BatchDeleteProductCustomFieldListColumnRequest{
			Ids: req.Ids,
		}); err != nil {
			handler.HandleErrorResponse(ctx, err)
			return
		}
		handler.HandleSuccessResponse(ctx)
	}
}

